package com.dl7.playerdemo;

import com.bumptech.glide.Glide;
import com.dl7.player.media.IjkPlayerView;
import com.dl7.playerdemo.utils.ScreenUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.StackLayout;
import ohos.agp.components.TabList;
import ohos.agp.window.service.WindowManager;
import ohos.global.configuration.Configuration;
import ohos.multimodalinput.event.KeyEvent;
import tv.danmaku.ijk.media.player.IMediaPlayer;

import java.io.IOException;
import java.io.InputStream;

public class SwitchVideoActivity extends Ability {

    private static final String IMAGE_URL = "http://vimg3.ws.126.net/image/snapshot/2015/5/J/M/VAPRJCSJM.jpg";

    private final String[] mVideoPath = new String[]{
            "http://flv2.bn.netease.com/videolib3/1505/29/DCNOo7461/SD/DCNOo7461-mobile.mp4",
            "http://flv2.bn.netease.com/videolib3/1611/28/nNTov5571/SD/nNTov5571-mobile.mp4",
            "https://newcntv.qcloudcdn.com/asp/hls/450/0303000a/3/default/84f142fa588d499d91ef249a01592d95/450.m3u8",
    };
    private final String[] mTitle = new String[]{
            "视频1", "视频2", "视频3",
    };

    private IjkPlayerView mPlayerView;
    private TabList mTagLayout;
    private int mIndex = 0;
    InputStream stream = null;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_switch_video);
        WindowManager.getInstance().getTopWindow().get().setTransparent(true);
        mPlayerView = (IjkPlayerView) findComponentById(ResourceTable.Id_switch_player_view);
        mTagLayout = (TabList) findComponentById(ResourceTable.Id_tag_layout);
        Glide.with(this).load(IMAGE_URL).fitCenter().into(mPlayerView.mPlayerThumb);
        try {
            stream = getContext()
                    .getResourceManager()
                    .getRawFileEntry("resources/rawfile/bili.xml").openRawFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        mPlayerView.init()
                .setTitle(mTitle[mIndex])
//                 屏蔽弹幕功能
//                .enableDanmaku()
//                .setDanmakuSource(stream)
                .setVideoPath(mVideoPath[mIndex]);
        mPlayerView.initDamukuView("resources/rawfile/bili.xml");
        mPlayerView.setOnCompletionListener(new IMediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(IMediaPlayer mp) {
                mIndex++;
                if (mIndex != mTitle.length) {
                    mPlayerView.switchVideoPath(mVideoPath[mIndex])
                            .setTitle(mTitle[mIndex])
                            .enableDanmaku(false)
                            .start();
                    mTagLayout.selectTabAt(mIndex);
                }
            }
        });

        initTabList();
        mTagLayout.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                if (mIndex != tab.getPosition()) {
                    mIndex = tab.getPosition();
                    mPlayerView.switchVideoPath(mVideoPath[mIndex])
                            .setTitle(mTitle[mIndex])
//                    屏蔽弹幕功能
//                            .enableDanmaku()
//                            .setDanmakuSource(stream)
                            .start();
                }
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });

    }

    private void initTabList() {
        TabList.Tab tab = mTagLayout.new Tab(getContext());
        tab.setText(mTitle[0]);
        mTagLayout.addTab(tab);

        TabList.Tab tab2 = mTagLayout.new Tab(getContext());
        tab2.setText(mTitle[1]);
        mTagLayout.addTab(tab2);

        TabList.Tab tab3 = mTagLayout.new Tab(getContext());
        tab3.setText(mTitle[2]);
        mTagLayout.addTab(tab3);

        mTagLayout.setFixedMode(true);
        mTagLayout.selectTab(tab);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mPlayerView.onDestroy();
    }

    @Override
    protected void onActive() {
        super.onActive();
        mPlayerView.onResume();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        mPlayerView.onPause();
    }

    @Override
    public void onConfigurationUpdated(Configuration newConfig) {
        int screenWidth = ScreenUtils.getScreenWidth(this);
        int screenHeight = ScreenUtils.getScreenHeight(this);

        super.onConfigurationUpdated(newConfig);

        if (newConfig.direction == Configuration.DIRECTION_HORIZONTAL) {
            mPlayerView.setLayoutConfig(new StackLayout.LayoutConfig(screenWidth, screenHeight));
        } else {
            mPlayerView.setLayoutConfig(new StackLayout.LayoutConfig(screenWidth, 600));
        }

        mPlayerView.configurationChanged(newConfig);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mPlayerView.handleVolumeKey(keyCode)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (mPlayerView.onBackPressed()) {
            return;
        }
        super.onBackPressed();
    }
}
